package com.cskaoyan.controller.mall;

import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.PageDataVo;
import com.cskaoyan.bean.mall.aftersale.AfterSale;
import com.cskaoyan.bean.mall.aftersale.AfterSaleStatus;
import com.cskaoyan.service.mall.AfterSaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @description: 商场管理模块——售后管理
 * @author: enhui
 **/
@RestController
@RequestMapping("admin/aftersale")
public class AfterSaleController {
    @Autowired
    AfterSaleService afterSaleService;

    @RequestMapping("list")
    public BaseRespVo afterSaleList(Short status, Integer id, String aftersaleSn, BaseParam param) {
        PageDataVo basePageData = afterSaleService.afterSaleList(status, id, aftersaleSn, param);
        return BaseRespVo.ok(basePageData);
    }

    /**
     * 接受，执行update 将status=1改为2
     * 售后状态，0是可申请，1是用户已申请，2是管理员审核通过，3是管理员退款成功，4是管理员审核拒绝，5是用户已取消
     *
     * @param afterSale
     * @return
     */
    @RequestMapping("recept")
    public BaseRespVo receive(@RequestBody AfterSale afterSale) {
//        AfterSale afterSaleReceive = afterSaleService.receive(afterSale);
        AfterSale afterSaleReceive = afterSaleService.update(afterSale, AfterSaleStatus.RECEIVED);
        if (afterSaleReceive == null) {
            return BaseRespVo.error("通过失败");
        }
        return BaseRespVo.ok(afterSaleReceive);
    }

    /**
     * 拒绝，执行update 将status=1改为4
     *
     * @param afterSale
     * @return
     */
    @RequestMapping("reject")
    public BaseRespVo reject(@RequestBody AfterSale afterSale) {
        AfterSale afterSaleReject = afterSaleService.update(afterSale, AfterSaleStatus.REJECTED);
        if (afterSaleReject == null) {
            return BaseRespVo.error("拒绝失败");
        }
        return BaseRespVo.ok(afterSaleReject);
    }

    /**
     * 退款，执行update 将2改为3
     *
     * @param afterSale
     * @return
     */
    @RequestMapping("refund")
    public BaseRespVo refund(@RequestBody AfterSale afterSale) {
        AfterSale afterSaleReject = afterSaleService.update(afterSale, AfterSaleStatus.REFUND_SUCCESS);
        if (afterSaleReject == null) {
            return BaseRespVo.error("退款失败");
        }
        return BaseRespVo.ok(afterSaleReject);
    }

    /**
     * 批量接受，只能接受用户已申请的，也就是status=1改为2
     *
     * @param map
     * @return
     */
    @RequestMapping("batch-recept")
    public BaseRespVo batchReceive(@RequestBody Map map) {
        List<Integer> ids = (List<Integer>) map.get("ids");
        List<AfterSale> afterSaleReceives = afterSaleService.batchUpdate(ids, AfterSaleStatus.RECEIVED);
        if (afterSaleReceives == null) {
            return BaseRespVo.error("批量通过失败");
        }
        return BaseRespVo.ok(afterSaleReceives);
    }

    /**
     * 批量拒绝，只能拒绝用户已申请的，也就是status=1改为4
     *
     * @param map
     * @return
     */
    @RequestMapping("batch-reject")
    public BaseRespVo batchReject(@RequestBody Map map) {
        List<Integer> ids = (List<Integer>) map.get("ids");
        List<AfterSale> afterSaleReceives = afterSaleService.batchUpdate(ids, AfterSaleStatus.REJECTED);
        if (afterSaleReceives == null) {
            return BaseRespVo.error("批量拒绝失败");
        }
        return BaseRespVo.ok(afterSaleReceives);
    }


}
